package com.thirtydays.frame.ui.text

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.SpanUtils
import com.thirtydays.frame.util.ext.getLayoutBitmap

/**
 * @author 小垚
 * @创建时间： 2021/5/22
 * @描述：首添加标签TextView
 **/

enum class Location {
    LOCATION_LEFT, LOCATION_RIGHT,
}

class TagTextView: AppCompatTextView{
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)


    fun setContentAndTag(
        content: String,
        layoutId: Int,
        padding: Int,
        drawables: List<Int>?,
        tags: List<String>,
        location: Location
    ) {
        if (tags.isEmpty() || drawables.isNullOrEmpty()) {
            text = content
            return
        }
        val with = SpanUtils.with(this)

        if (location == Location.LOCATION_LEFT) {
            tags.forEachIndexed { index, it ->
                val infalate = LayoutInflater.from(context).inflate(layoutId, null, false) as TextView
                infalate.text = it
                drawables?.let {
                    infalate.setBackgroundResource(it[index])
                }
                val bitmap = infalate.getLayoutBitmap()
                with.appendImage(bitmap)
                    .appendSpace(padding)
            }
            with.append(content)
        } else {
            with.append(content)
            tags.forEachIndexed { index, it ->
                val tvTag = LayoutInflater.from(context).inflate(layoutId, null, false) as TextView
                tvTag.text = it
                drawables?.let {
                    tvTag.setBackgroundResource(it[index])
                }
                val bitmap = tvTag.getLayoutBitmap()
                with.appendImage(bitmap)
                    .appendSpace(padding)
            }

        }
        with.create()
    }
}